package com.icss.company.controller;

import com.icss.company.pojo.Dept;
import com.icss.company.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/listEmp")
    public Map<String,Object> list(String dname){
        /*System.out.println(1/0);*/
        Map<String,Object> result = new HashMap<>();
        result.put("code","200");
        result.put("msg","操作成功");
        result.put("data",deptService.list(dname));
        return result;
    }
    // /dept/del?deptno=10
    //restful  /dept/del/10
    @GetMapping("/del")
    public Map<String,Object> del(Integer deptno){
        Map<String,Object> result = new HashMap<>();
        if(deptService.del(deptno)){
            result.put("code","200");
            result.put("msg","操作成功");
        }else{
            result.put("code","500");
            result.put("msg","操作失败");
        }
        return result;
    }

    @GetMapping("/del/{deptno}")
    public Map<String,Object> del1(@PathVariable("deptno") Integer deptno){
        Map<String,Object> result = new HashMap<>();
        if(deptService.del(deptno)){
            result.put("code","200");
            result.put("msg","操作成功");
        }else{
            result.put("code","500");
            result.put("msg","操作失败");
        }
        return result;
    }

    @PostMapping("/add")
    public Map<String,Object> add(Dept dept){
        Map<String,Object> result = new HashMap<>();
        if(deptService.add(dept)){
            result.put("code","200");
            result.put("msg","操作成功");
        }else{
            result.put("code","500");
            result.put("msg","操作失败");
        }
        return result;
    }

    @GetMapping("/get/{deptno}")
    public Map<String,Object> get(@PathVariable("deptno") Integer deptno) {
        Map<String, Object> result = new HashMap<>();
        result.put("code","200");
        result.put("msg","操作成功");
        result.put("data",deptService.get(deptno));
        return result;
    }

    @PostMapping("/update")
    public Map<String,Object> update(Dept dept){
        Map<String,Object> result = new HashMap<>();
        if(deptService.update(dept)){
            result.put("code","200");
            result.put("msg","操作成功");
        }else{
            result.put("code","500");
            result.put("msg","操作失败");
        }
        return result;
    }

}
